<?php

namespace work_queues;
require_once __DIR__ . '/../vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
class confirm_message
{
    const MessageCount = 1000;
    public function __construct()
    {
        // 1、单个确认


        // 2、批量确认

        // 3、异步批量确认
    }

    //单个确认
    public static function confirm_message()
    {
        $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
        $channel = $connection->channel();
        // 开启信道确认发布
        $channel->confirm_select(true);
        $channel->queue_declare('task_queue', false, true, false, false);


        $argv = [];
        $data = implode(' ', array_slice($argv, 1));
        if(empty($data)) $data = "Hello World!";
        $msg = new AMQPMessage($data,
            array('delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT)
        );

        // $channel->basic_publish($msg, '', 'task_queue');
        // 开始时间
        $begin = microtime(true);
        // 批量发消息
        for ($i = 0; $i < self::MessageCount; $i++) {
            $message = $i . '';
            $channel->basic_publish($message, '', 'task_queue');
            // 单个消息就马上进行发布确认

        }
        echo " [x] Sent ", $data, "\n";

        $channel->close();
        $connection->close();
    }
}

new confirm_message();